#pragma once
#include "stdafx.h"
#include "scene/Camera.h"
#include "scene/Menu.h"
#include "io/InputDevice.h"
#include "game/Resource.h"
#include "math/math.h"
#include "math/Coordinate.h"
#include "game/Pickup.h"
#include "scene/World.h"
#include "game/FigureObject.h"
#include "render/render.h"
#include "render/RenderManager.h"

/*#include "game/story.h"
#include "scene/World.h"
#include "render/Texture.h"
#include "Error.h"
#include "render/Text.h"
*/

namespace SLG
{
	
	/*
 #define D3DFVF_TEXTURE	(D3DFVF_XYZRHW | D3DFVF_TEX1)
 #define D3DFVF_VERTEX	(D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
 #define D3DFVF_LINE		(D3DFVF_XYZRHW )

 struct CUSTOM_TEXTURE
 {
 FLOAT	fX,fY,fZ,fRHW;
 FLOAT tu, tv;   // The texture coordinates
 };

 struct CUSTOM_VERTEX
 {
 CUSTOM_VERTEX()
 {
 }
 CUSTOM_VERTEX(float x,float y,float z,float rhw,float color)
 :fX(x),fY(y),fZ(z),fRHW(rhw),dwColor(color)
 {
 }
 float	fX,fY,fZ,fRHW;
 DWORD	dwColor;
 };

 struct CUSTOM_LINE
 {
 FLOAT	fX,fY,fZ,fRHW;
 };

 struct Render_FORMAT
 {
 LPDIRECT3DVERTEXBUFFER9 pVERTEX;
 LPDIRECT3DTEXTURE9 pTEXTURE;
 LPDIRECT3DINDEXBUFFER9 pINDEX;
 D3DPRIMITIVETYPE ePRIMITIVE;	
 DWORD FVF;
 DWORD dwVERTEXCOUNT;
 DWORD dwVERTEXSIZE;
 };
 struct Render_LIST
 {
 Render_FORMAT* RenderStr;
 Render_LIST* next;
 Render_LIST* previous;
 };*/

	class SLGEngine
	{
	public:
		SLGEngine();
		~SLGEngine(void);

		bool Init();
		bool InitData();
		void NewWindow();
		static LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
		HRESULT InitD3D();
		bool InitDevice();
		bool InitRes();
		bool InitScene();
		bool InitRender();
		bool InitFight();
		void GameLoop();
		void GameProc();
		void UpdateGame();

		void GraphRender();
		bool InitFightData();
		void BuildScene();
		void VisibiltyDetection(const D3DXMATRIX*);
		void RenderStatus();
		void CreateGeometry();
		void Transformation();
		void Light();
		void BackCulling();
		void Clipping();
		void Projection();
		void Rasterization();
		void Coloring();
		void RenderScreen();
		void SetCamera();

		void CameraW();
		void CameraQ();
		void CameraE();
		void CameraA();
		void CameraS();
		void CameraD();

		void Esc();

		void MouseLeftClick();

		LPDIRECT3DDEVICE9 GetDevice();

		void ChangeGameState(GAME_STATE);
		void MouseMove();
		uint32 MouseCollectJudge();


	private:
		int m_screenWidth;
		int m_screenHeight;

		HWND m_hWnd;							//Window handle.
		LPDIRECT3D9 m_pD3D;
		
		D3DCAPS9 m_caps;
		D3DPRESENT_PARAMETERS m_presentParameters;
		

		DWORD m_lastTime;
		DWORD m_incTime;

		GAME_STATE	m_eGState;
		Camera* m_camera;
		InputDevice* m_ind;
		render* m_render;
		Resource* m_res;							//Resource manager.

		
		RenderManager* m_rManager;

		
		Pickup* m_pick;
		Menu* m_menu;
		World* m_world;
		FightLogic* m_fLogic; 
		
		const D3DXMATRIX* m_cameraView;

		D3DXVECTOR2 m_mousePoint;

		bool m_bStory;
		bool m_bFight;

		//AABB* m_world;

		/*VOID CleanScreen();
		HRESULT BeginScene();
		HRESULT EndScene();
		HRESULT Present();

		






		Render_LIST* AddRender(Render_FORMAT*);
		HRESULT RenderList();
		HRESULT RenderLine(D3DXVECTOR3*);
		VOID ClearRender();

		BOOL CreateVertexBuffer(VOID* vertex,size_t size,DWORD FVF, LPDIRECT3DVERTEXBUFFER9* pVB);
		BOOL CreateIndexBuffer(VOID* indices,size_t size, LPDIRECT3DINDEXBUFFER9* pIB);

		
		
		Text m_text;
		

		Texture* m_tex;
		Error* m_err;



		LPDIRECT3DVERTEXBUFFER9 m_pCursor;

		Render_LIST m_list;
		Render_LIST* m_rlp;	

		//HRESULT CreateRange(VOID* cv, size_t size, DWORD fvf, IDirect3DVertexBuffer9* pVertexBuffer);
		//VOID RenderSystem();


		SLGEngine(void);
		~SLGEngine(void);

		BOOL ReadStory();
		VOID UpdateStory();
		VOID DrawStory(const char*);
		VOID DrawCursor();

		VOID AddFigure(int,int,int);
		VOID DelFigure(int);
		VOID AddStoryRenderList();

		VOID CreateRange();




		VOID UpdateStoryTexture();

		VOID Render();
		VOID NextScene();
		VOID CleanTexture();

		VOID ChangeToFight();

		VOID MouseMove(D3DXVECTOR2);
		D3DXVECTOR2 m_mousePoint;

		VOID KeyBoardEvent();

		Story m_story;
		BOOL m_autoStory;

		World* m_world;

		const char* m_dialog;



		LPDIRECT3DVERTEXBUFFER9 m_pPersonLeft;
		LPDIRECT3DVERTEXBUFFER9 m_pPersonRight;
		LPDIRECT3DVERTEXBUFFER9 m_pDialogBox;
		LPDIRECT3DVERTEXBUFFER9 m_pTextRange;
		LPDIRECT3DVERTEXBUFFER9 m_pCursor;
		LPDIRECT3DVERTEXBUFFER9 m_pBackground;

		LPDIRECT3DTEXTURE9 m_TextureBackground;
		LPDIRECT3DTEXTURE9 m_TextureCursor;
		LPDIRECT3DTEXTURE9 m_TextureLeftPerson;
		LPDIRECT3DTEXTURE9 m_TextureRightPerson;

		BOOL DrawBasicScene( float scale);*/
	};
}